<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <label for="file">上传图片格式验证:</label>
    <input type="file" id="file">
</body>
<script>
    //jpg png gif jpeg
    var file=document.getElementById('file');
    file.onchange=function(){
        //获取图片路径
        var path=this.value;
        //获取.在路径字符串中的位置
        var loc=path.lastIndexOf('.');
        //截取文件路径的后缀名
        var suffix=path.substr(loc);
        //将后缀名改为小写
        var low_suffix=suffix.toLowerCase();
        //开始判断
        if(low_suffix==='.jpg'||low_suffix==='.png'||low_suffix==='.gif'||low_suffix==='.jpeg'){
            alert('上传格式正确');
        }else{
            alert('上传格式错误');
        }
    }
     
</script>
</html>